package com.qzsoft.zhjd.materiel.modules.materielweighttype.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qzsoft.jeemis.platform.security.user.SecurityUser;
import com.qzsoft.jeemis.platform.security.user.UserDetail;
import com.qzsoft.zhjd.common.components.BaseServiceComponent;
import com.qzsoft.zhjd.common.enums.BizCode;
import com.qzsoft.zhjd.common.utils.RenExceptionUtils;
import com.qzsoft.zhjd.materiel.modules.materielweighttype.dto.MaterielWeightTypeDto;
import com.qzsoft.zhjd.materiel.modules.materielweighttype.entity.MaterielWeightTypeEntity;
import com.qzsoft.zhjd.materiel.modules.materielweighttype.mapper.MaterielWeightTypeMapper;
import com.qzsoft.zhjd.materiel.modules.materielweighttype.service.MaterielWeightTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service("materielWeightTypeService")
public class MaterielWeightTypeServiceImpl extends ServiceImpl<MaterielWeightTypeMapper, MaterielWeightTypeEntity> implements MaterielWeightTypeService {

    @Autowired
    private BaseServiceComponent baseServiceComponent;

    @Autowired
    private MaterielWeightTypeService weightTypeService;

//    @Autowired
//    private SysDictService sysDictService;
//
//    @Autowired
//    private MaterielApiVesionService materielApiVesionService;
//
//    @Autowired
//    private MaterielApiService materielApiService;
//
//    @Value("${zhjd.upload.getMaterialWeightType}")
//    private String getMaterialWeightType;

    @Override
    public void saveTypeInfo(MaterielWeightTypeDto weightTypes) {
        QueryWrapper<MaterielWeightTypeEntity> weightEntity = new QueryWrapper<>();
        weightEntity.eq("weight_data_type",weightTypes.getWeightDataType());
        Integer number = weightTypeService.count(weightEntity);
        //获取登录用户信息
        UserDetail user = SecurityUser.getUser();
        //判断过磅类型数据是否已经存在
        if(number != null && number > 0){
            RenExceptionUtils.throwEx( "该过磅类型"+weightTypes.getWeightDataType()+"已经存在！");
        }
        //存入过磅类型数据
        MaterielWeightTypeEntity weightType = new MaterielWeightTypeEntity();
        weightType.setWeightType(weightTypes.getWeightType());
        weightType.setWeightDataType(weightTypes.getWeightDataType());
        weightType.setSort(weightTypes.getSort());
        weightType.setName(weightTypes.getName());
        weightType.setSort(weightTypes.getSort());
        weightType.setEnable(weightTypes.getEnable());
        weightType.setCreator(user.getId());
        weightType.setCreatorName(user.getName());
        weightType.setUpdater(user.getId());
        weightType.setUpdateName(user.getName());
        try {
            weightTypeService.save(weightType);
        } catch (Exception e) {
            RenExceptionUtils.throwEx(BizCode.EXCEPTION, "过磅类型数据新增失败！");
        }
    }

//    @Override
//    public IPage<MaterielWeightTypeEntity> listInfo(Map<String, Object> params) {
//        List<MaterielWeightTypeEntity> weightLists = new ArrayList<MaterielWeightTypeEntity>();
//        QueryWrapper<MaterielWeightTypeEntity> queryWrapper = new QueryWrapper<MaterielWeightTypeEntity>();
//        queryWrapper.select("id","weight_type","weight_data_type","name","sort","enable","update_name","update_date").orderByAsc("sort");
//
//        Integer current = Integer.parseInt(String.valueOf(params.get("current")));
//        Integer size = Integer.parseInt(String.valueOf(params.get("size")));
//
//        if(!params.containsKey("key") || ("".equals(params.get("key")))){
//            //查询全部信息
//            weightLists = weightTypeService.list(queryWrapper);
//        }else{
//            //通过磅单类型查询
//            String key = String.valueOf(params.get("key"));
//            queryWrapper.eq("weight_type",key);
//            weightLists = weightTypeService.list(queryWrapper);
//        }
//        Long total =(long) weightLists.size();
//        //进行分页处理
//        weightLists = pageByParams(current,size,weightLists);
//        //Ipage返回数据信息填充
//        IPage<MaterielWeightTypeEntity> pageWeightTypes = baseServiceComponent.getPage(params, MaterielWeightTypeEntity.class);
//        pageWeightTypes.setRecords(weightLists);
//        pageWeightTypes.setTotal((long) total);
//        return pageWeightTypes;
//    }

    @Override
    public MaterielWeightTypeEntity infoDetails(String id) {
        MaterielWeightTypeEntity weightType = weightTypeService.getById(id);
        return weightType;
    }

    @Override
    public void updateTypeInfo(MaterielWeightTypeDto weightTypesDto) {
        UserDetail user = SecurityUser.getUser();
        MaterielWeightTypeEntity weightType = new MaterielWeightTypeEntity();

        QueryWrapper<MaterielWeightTypeEntity> weightEntity = new QueryWrapper<>();
        weightEntity.eq("id", weightTypesDto.getId());
        Integer number = weightTypeService.count(weightEntity);
        if(number != null && number == 0){
            RenExceptionUtils.throwEx( "该过磅类型"+weightTypesDto.getWeightDataType()+"不存在！");
        }
        //给更新数据赋值
        weightType.setWeightType(weightTypesDto.getWeightType());
        weightType.setWeightDataType(weightTypesDto.getWeightDataType());
        weightType.setSort(weightTypesDto.getSort());
        weightType.setName(weightTypesDto.getName());
        weightType.setSort(weightTypesDto.getSort());
        weightType.setEnable(weightTypesDto.getEnable());
        weightType.setUpdater(user.getId());
        weightType.setUpdateName(user.getName());

        try {
            weightTypeService.update(weightType,weightEntity);
        }catch(Exception e){
            RenExceptionUtils.throwEx(BizCode.EXCEPTION, "过磅类型数据"+weightTypesDto.getWeightDataType()+"更新失败！");
        }
    }

    @Override
    public IPage<MaterielWeightTypeDto> listByPage(Map<String, Object> params) {
        IPage<MaterielWeightTypeDto> page = baseServiceComponent.getPage(params, MaterielWeightTypeDto.class);
        return baseMapper.queryDto(page, params);
    }

//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public void getUrlInfo() throws IOException {
//        //获取访问接口的url
//        String url = getMaterialWeightType;
//        Map<String,Object> params = new HashMap<String,Object>();
//        Long getWeightType = materielApiVesionService.getVersionByCode("getWeightType");
//        params.put("getWeightType",getWeightType);
//        //发送请求
//        String result = HttpUtil.get(url,params);
//        JSONObject resultJson = JSONObject.parseObject(result);
//        if(resultJson == null){
//            RenExceptionUtils.throwEx(BizCode.EXCEPTION, "传输数据格式不正确");
//        }
//        Integer status = resultJson.getInteger("code");
//        if(status != 200){
//            RenExceptionUtils.throwEx(BizCode.EXCEPTION, "请求失败");
//        }
//        JSONArray datas = resultJson.getJSONArray("data");
//        if(datas == null){
//            RenExceptionUtils.throwEx(BizCode.EXCEPTION, "返回数据格式异常");
//        }
//        //获取过磅类型数据
//        List<RequestWeightTypeDto> weightTypes = datas.toJavaList(RequestWeightTypeDto.class);
//        //更新version
//        materielApiVesionService.updateByCode("getWeightType");
//        //删除表中数据
//        QueryWrapper<BasicCodeitemEntity> queryWrapper = new QueryWrapper<BasicCodeitemEntity>();
//        queryWrapper.eq("codeid", "WEIGHT_TYPE");
//        queryWrapper.notLike("pptr","WEIGHT_TYPE");
//        sysDictService.remove(queryWrapper);
//        //存储过磅类型数据
//        for(RequestWeightTypeDto weightType : weightTypes){
//            BasicCodeitemEntity weightTypeEntity = new BasicCodeitemEntity();
//            weightTypeEntity.setCodeid("WEIGHT_TYPE");
//            weightTypeEntity.setPptr(weightType.getPid());
//            weightTypeEntity.setCode(weightType.getId());
//            weightTypeEntity.setDescription(weightType.getName());
//            sysDictService.save(weightTypeEntity);
//        }
//    }

//    private List<MaterielWeightTypeEntity>   pageByParams(Integer page, Integer rows, List<MaterielWeightTypeEntity> list) {
//        int begin = (page - 1) * rows;
//        int total = list.size();
//        if (begin > total) {
//            return new ArrayList<>();
//        }
//        int end = page * rows;
//        end = end < total ? end : total;
//        return list.subList(begin, end);
//    }
}
